#
# Copyright 2009 Red Hat, Inc. and/or its affiliates.
#
# Licensed to you under the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.  See the files README and
# LICENSE_GPL_v2 which accompany this distribution.
#

import unittest as ut
import uuid

import basicVdsTest as vdsclient

NFS_SERVER_TYPE = 1

DATA_CLASS = 1
ISO_CLASS = 2

def validateConfig(cfg):
    '''Validate configuration "cfg" (ConfigParser) to be complete and coherent
    '''
    valid, reason = False, "Configuration is broken"

    if cfg.has_section('nfs'):
        valid = True
        reason = "Configuration valid"
        if cfg.has_option('nfs', 'server1'):
            if not cfg.has_option('nfs', 'domain1'):
                cfg.set('nfs', 'domain1', uuid.generate())

        if cfg.has_option('nfs', 'server2'):
            if not cfg.has_option('nfs', 'domain2'):
                cfg.set('nfs', 'domain2', uuid.generate())

        if cfg.has_option('nfs', 'server3'):
            if not cfg.has_option('nfs', 'domain3'):
                cfg.set('nfs', 'domain3', uuid.generate())

    return valid, reason


class SimpleNFSTest(vdsclient.BasicVDSTest):
    '''
    SimpleNFSTest validates the NFS server connection functionality.
    '''
    @classmethod
    def tests(cls):
        return [
            "ConnectNFSServer",
            "DoubleConnectNFSServer",
            "DisconnectNFSServer",
            "DoubleDisconnectNFSServer",
            ]


    def connectNFS(self):
        s, resp = self.connectStorageServer(NFS_SERVER_TYPE, self.connectionList)

        return s

    def disconnectNFS(self):
        s, resp = self.disconnectStorageServer(NFS_SERVER_TYPE, self.connectionList)

        return s

    def setUp(self):
        # First setup local environment
        self.server1 = self.cfg.get('nfs', 'server1')
        self.server2 = self.cfg.get('nfs', 'server2')
        self.server3 = self.cfg.get('nfs', 'server3')

        self.sdUUID1 = self.cfg.get('nfs', 'domain1')
        self.sdUUID2 = self.cfg.get('nfs', 'domain2')
        self.sdUUID3 = self.cfg.get('nfs', 'domain3')

        self.connectionList = [
            {'id':1, 'connection':self.server1},
            {'id':2, 'connection':self.server2},
            {'id':3, 'connection':self.server3}
        ]


        self.debug = self.cfg.getboolean('nfs', 'debug')

        vdsclient.BasicVDSTest.setUp(self)
        self.connectNFS()


    def tearDown(self):
        vdsclient.BasicVDSTest.tearDown(self)


    def ConnectNFSServer(self):
        self.assertTrue(self.connectNFS())


    def DoubleConnectNFSServer(self):
        self.assertTrue(self.connectNFS())


    def DisconnectNFSServer(self):
        self.assertTrue(self.disconnectNFS())


    def DoubleDisconnectNFSServer(self):
        self.assertTrue(self.disconnectNFS())


class BasicNFSTest(SimpleNFSTest):
    '''
    BasicNFSTest validates NFS Storage Domain functionality
    '''
    @classmethod
    def tests(cls):
        return ([
            "CreateStorageDomain1",
            "CreateStorageDomain2",
            "CreateISOStorageDomain",
            "FormatStorageDomain1",
            "FormatStorageDomain2",
            "FormatISOStorageDomain"
            ])


    def createSD(self, sdUUID, description, physical, domclass):
        s, resp = self.createStorageDomain(NFS_SERVER_TYPE, sdUUID,
            description, physical, domclass)

        return resp


    def formatSD(self, sdUUID):
        s, resp = self.formatStorageDomain(sdUUID)

        return resp


    def setUp(self):
        SimpleNFSTest.setUp(self)
        self.connectNFS()


    def tearDown(self):
        self.disconnectNFS()
        SimpleNFSTest.tearDown(self)


    def CreateStorageDomain1(self):

        resp = self.createSD(self.sdUUID1, "D1", self.server1, DATA_CLASS)

        self.assertOK(resp)


    def CreateStorageDomain2(self):

        resp = self.createSD(self.sdUUID2, "D2", self.server2, DATA_CLASS)

        self.assertOK(resp)


    def CreateISOStorageDomain(self):

        resp = self.createSD(self.sdUUID3, "D3", self.server3, ISO_CLASS)

        self.assertOK(resp)


    def FormatStorageDomain1(self):

        resp = self.formatSD(self.sdUUID1)

        self.assertOK(resp)


    def FormatStorageDomain2(self):

        resp = self.formatSD(self.sdUUID2)

        self.assertOK(resp)


    def FormatISOStorageDomain(self):

        resp = self.formatSD(self.sdUUID3)

        self.assertOK(resp)


class AdvancedNFSTest(BasicNFSTest):

    @classmethod
    def tests(cls):
        return ([
            "GetStorageDomainInfo",
            ])

    def setUp(self):
        BasicNFSTest.setUp(self)
        resp = self.createSD(self.sdUUID1, "D1", self.server1, DATA_CLASS)
        resp = self.createSD(self.sdUUID2, "D2", self.server2, DATA_CLASS)
        resp = self.createSD(self.sdUUID3, "D3", self.server3, ISO_CLASS)


    def tearDown(self):
        resp = self.formatSD(self.sdUUID1)
        resp = self.formatSD(self.sdUUID2)
        resp = self.formatSD(self.sdUUID3)
        BasicNFSTest.tearDown(self)

    def GetStorageDomainInfo(self):
        s, resp = self.getStorageDomainInfo(self.sdUUID1)
        self.assertOK(resp)

def suite():
    # Load everything works like that:
    # basicsuite = ut.TestLoader().loadTestsFromTestCase(BasicNFSTest)

    simplesuite = ut.TestSuite(map(SimpleNFSTest, SimpleNFSTest.tests()))
    basicsuite = ut.TestSuite(map(BasicNFSTest, BasicNFSTest.tests()))
    advancedsuite = ut.TestSuite(map(AdvancedNFSTest, AdvancedNFSTest.tests()))

    suite = ut.TestSuite([simplesuite, basicsuite, advancedsuite])

    return suite
